package com.ydkj.a37e_mall.Classes.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ydkj.a37e_mall.R;

/**
 * Created by Administrator on 2017/1/13.
 * 订单配送
 */

public class DistributionOrderFg extends BaseFg implements View.OnClickListener {
    private View rootView;
    private Activity mActivity;
    private TextView tv_distribution_order_all, tv_distribution_order_ect, tv_distribution_order_complete, tv_distribution_order_abnormal;
    private ALLDistributionOrderFg allDistributionOrderFg;//完成
    private EctDistributionOrderFg ectDistributionOrderFg;//待配送
    private CompDistributionOrderFg compDistributionOrderFg;//已完成
    private AbnormalDistributionOrderFg abnormalDistributionOrderFg;//异常
    private FragmentManager fragmentManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.distribution_order, container, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        init();
    }

    private void init() {
        tv_distribution_order_all = (TextView) mActivity.findViewById(R.id.tv_distribution_order_all);
        tv_distribution_order_ect = (TextView) mActivity.findViewById(R.id.tv_distribution_order_ect);
        tv_distribution_order_complete = (TextView) mActivity.findViewById(R.id.tv_distribution_order_complete);
        tv_distribution_order_abnormal = (TextView) mActivity.findViewById(R.id.tv_distribution_order_abnormal);

        fragmentManager = getChildFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        allDistributionOrderFg = new ALLDistributionOrderFg();
        ectDistributionOrderFg = new EctDistributionOrderFg();
        compDistributionOrderFg = new CompDistributionOrderFg();
        abnormalDistributionOrderFg = new AbnormalDistributionOrderFg();

        ft.add(R.id.fly_disorder, allDistributionOrderFg, "one");
        ft.commitAllowingStateLoss();

        tv_distribution_order_all.setSelected(true);
        tv_distribution_order_all.setOnClickListener(this);
        tv_distribution_order_ect.setOnClickListener(this);
        tv_distribution_order_complete.setOnClickListener(this);
        tv_distribution_order_abnormal.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction ft;
        switch (view.getId()) {
            case R.id.tv_distribution_order_all://全部
                tv_distribution_order_all.setSelected(true);
                tv_distribution_order_ect.setSelected(false);
                tv_distribution_order_complete.setSelected(false);
                tv_distribution_order_abnormal.setSelected(false);

                ft = fragmentManager.beginTransaction();


                if (ectDistributionOrderFg.isAdded()) {
                    ft.hide(ectDistributionOrderFg);
                    ectDistributionOrderFg.onPause();
                }
                if (compDistributionOrderFg.isAdded()) {
                    ft.hide(compDistributionOrderFg);
                    compDistributionOrderFg.onPause();
                }
                if (abnormalDistributionOrderFg.isAdded()) {
                    ft.hide(abnormalDistributionOrderFg);
                    abnormalDistributionOrderFg.onPause();
                }

                ft.show(allDistributionOrderFg);
                ft.commit();
                break;
            case R.id.tv_distribution_order_ect://代配送
                tv_distribution_order_all.setSelected(false);
                tv_distribution_order_ect.setSelected(true);
                tv_distribution_order_complete.setSelected(false);
                tv_distribution_order_abnormal.setSelected(false);

                ft = fragmentManager.beginTransaction();

                if (allDistributionOrderFg.isAdded()) {
                    ft.hide(allDistributionOrderFg);
                    allDistributionOrderFg.onPause();
                }
                if (compDistributionOrderFg.isAdded()) {
                    ft.hide(compDistributionOrderFg);
                    compDistributionOrderFg.onPause();
                }
                if (abnormalDistributionOrderFg.isAdded()) {
                    ft.hide(abnormalDistributionOrderFg);
                    abnormalDistributionOrderFg.onPause();
                }

                if (ectDistributionOrderFg.isAdded()) {
                    ft.show(ectDistributionOrderFg);
                } else {
                    ft.add(R.id.fly_disorder, ectDistributionOrderFg, "two");
                    ft.show(ectDistributionOrderFg);
                }
                ft.commit();

                break;
            case R.id.tv_distribution_order_complete://已完成
                tv_distribution_order_all.setSelected(false);
                tv_distribution_order_ect.setSelected(false);
                tv_distribution_order_complete.setSelected(true);
                tv_distribution_order_abnormal.setSelected(false);


                ft = fragmentManager.beginTransaction();

                if (allDistributionOrderFg.isAdded()) {
                    ft.hide(allDistributionOrderFg);
                    allDistributionOrderFg.onPause();
                }
                if (ectDistributionOrderFg.isAdded()) {
                    ft.hide(ectDistributionOrderFg);
                    ectDistributionOrderFg.onPause();
                }
                if (abnormalDistributionOrderFg.isAdded()) {
                    ft.hide(abnormalDistributionOrderFg);
                    abnormalDistributionOrderFg.onPause();
                }

                if (compDistributionOrderFg.isAdded()) {
                    ft.show(compDistributionOrderFg);
                } else {
                    ft.add(R.id.fly_disorder, compDistributionOrderFg, "two");
                    ft.show(compDistributionOrderFg);
                }
                ft.commit();
                break;
            case R.id.tv_distribution_order_abnormal://异常
                tv_distribution_order_all.setSelected(false);
                tv_distribution_order_ect.setSelected(false);
                tv_distribution_order_complete.setSelected(false);
                tv_distribution_order_abnormal.setSelected(true);


                ft = fragmentManager.beginTransaction();

                if (allDistributionOrderFg.isAdded()) {
                    ft.hide(allDistributionOrderFg);
                    allDistributionOrderFg.onPause();
                }
                if (ectDistributionOrderFg.isAdded()) {
                    ft.hide(ectDistributionOrderFg);
                    ectDistributionOrderFg.onPause();
                }
                if (compDistributionOrderFg.isAdded()) {
                    ft.hide(compDistributionOrderFg);
                    compDistributionOrderFg.onPause();
                }

                if (abnormalDistributionOrderFg.isAdded()) {
                    ft.show(abnormalDistributionOrderFg);
                } else {
                    ft.add(R.id.fly_disorder, abnormalDistributionOrderFg, "two");
                    ft.show(abnormalDistributionOrderFg);
                }
                ft.commit();
                break;
        }
    }
}
